<?php
/*
 * 2017-0913
 */
class action extends app{
	public function display(){
		$this->tpl->assign('ListSearch',$this->search->getAll());
		$action = $this->routes->url(3);
		if(!method_exists($this,$action))
		$action = "index";
		$this->$action();
		exit;
	}

	private function index(){
        if($this->G->loadclass('apps')->getOneByName('links')&&is_file(APP_PATH.'links/install.lock')){
            $friendlink=$this->G->loadclass('friendlink','links')->getAll(array('isview'=>1));
            $this->tpl->assign('friendlink',$friendlink);
        }
        $this->tpl->assign('ArtList',$this->article->getAll());
        $WebConfig=$this->G->loadclass('webconfig')->getWebConfig();
        $this->tpl->assign('SeoTitle',$WebConfig['website']['seo_title']);
		$this->tpl->assign('SeoKeywords',$WebConfig['website']['seo_keywords']);
		$this->tpl->assign('SeoDescription',$WebConfig['website']['seo_description']);
		$this->tpl->display('index');
	}

	private function category(){
		$page = intval($this->routes->get('page'));
		$page = $page>0?$page:1;
		$Catalog=$this->routes->get('catalog_name');
		$MyCat=$this->category->getOneByCatalog($Catalog);
		if($MyCat){
			$ListApps=$this->apps->getAllByData();
			$cids=$this->category->getCids(array('parent_id'=>$MyCat['id']));
			if($cids){
				$cids=$cids.','.$MyCat['id'];
			}else{
				$cids=$MyCat['id'];
			}
			$condition="FIND_IN_SET(cat_id,'".$cids."')";
			$ListArt=$this->article->getList($page,$MyCat['cat_pagenum'],$condition,'list');
			if($page>$ListArt['total']&&$page>1){
				$message = array(
					'statusCode' =>300,
					"message" => "请勿乱策",
					"callbackType" =>'forward',
					"forwardUrl" =>"goback"
				);
				$this->G->R($message);
			}
			$NavCrumb=$this->category->getNavCrumbByCid($MyCat['id']);
			$ListCatSun=$this->category->getSunByCid($MyCat['id']);
			if(empty($ListCatSun)){
				if($MyCat['parent_id']==0){
					$ListCatSun=$this->category->getAll(array('id'=>$MyCat['id']));
				}else{
					$ListCatSun=$this->category->getSunByCid($MyCat['parent_id']);
				}
			}
			$ListArtHot=$this->article->getSpecialArt($MyCat['id'],$MyCat['cat_pagenum'],'view desc');
			$ListArtNew=$this->article->getSpecialArt($MyCat['id'],$MyCat['cat_pagenum'],'addtime desc');
			$this->tpl->assign("ListArtNew",$ListArtNew);
			$this->tpl->assign("ListArtHot",$ListArtHot);
			$this->tpl->assign("ListApps",$ListApps);		
			$this->tpl->assign("MyCat",$MyCat);
			$this->tpl->assign("NavCrumb",$NavCrumb);
			$this->tpl->assign("Catalog",$Catalog);
			$this->tpl->assign("ListArt",$ListArt);
			$this->tpl->assign("ListCatSun",$ListCatSun);
			$SeoTitle=$MyCat['seo_title']!=''?$MyCat['seo_title']:$MyCat['cat_name'];
			$SeoKeywords=$MyCat['seo_keywords']!=''?$MyCat['seo_keywords']:$MyCat['cat_name'];
			$SeoDescription=$MyCat['seo_description']!=''?$MyCat['seo_description']:$MyCat['cat_name'];
			if($page>1){
				$this->tpl->assign("SeoTitle",$SeoTitle.' 第'.$page.'页');
			}else{
				$this->tpl->assign("SeoTitle",$SeoTitle);
			}
			$this->tpl->assign("SeoKeywords",$SeoKeywords);
			$this->tpl->assign("SeoDescription",$SeoDescription);
            if(empty($MyCat['cat_skin'])){
                $MyCat['cat_skin']='category_view';
            }
            $this->tpl->display($MyCat['cat_skin']);
		}else{
			header("location:/404.html");
		}			
	}

	private function article(){
		$IntID=$this->routes->get('id');
		$Catalog=$this->routes->get('catalog_name');
		$MyCat=$this->category->getOneByCatalog($Catalog);
		if($Catalog==$MyCat['catalog_name']){
			$IntID = $IntID > 0?$IntID:1;
			$artme = $this->article->getOneByID($IntID);
			$Content=$artme['content'];
            if($this->apps->getOneByName('links')){
                $SetLink=$this->search->setInLinkByContent($Content);//加载内链
                if($SetLink){
                    $artme['content']=$SetLink;
                }
            }
			$this->article->updateView($IntID);
			$this->tpl->assign("ListArtNew",$this->article->getSpecialArt($MyCat['id'],$MyCat['cat_pagenum'],'addtime desc'));
			$this->tpl->assign("ListArtHot",$this->article->getSpecialArt($MyCat['id'],$MyCat['cat_pagenum'],'view desc'));
			$ListCatSun=$this->category->getSunByCid($MyCat['id']);
			if(empty($ListCatSun)){
				if($MyCat['parent_id']==0){
					$ListCatSun=$this->category->getAll(array('id'=>$MyCat['id']));
				}else{
					$ListCatSun=$this->category->getSunByCid($MyCat['parent_id']);
				}
			}
			$this->tpl->assign("ListCatSun",$ListCatSun);
			$this->tpl->assign('preArt',$this->article->getPreByID($IntID));//上一篇
			$this->tpl->assign('nextArt',$this->article->getNextByID($IntID));//下一篇
			$this->tpl->assign('NavCrumb',$this->article->getNavCrumbByAid($IntID));//面包屑导航
			$this->tpl->assign('RelationArt',$this->tags->getRelationArtByAid($IntID));//相关文章
			$this->tpl->assign('HotArt',$this->article->getSpecialArt($MyCat['id'],$MyCat['cat_pagenum'],'view desc'));//热门文章
            $this->tpl->assign('stags',$this->tags->getTagByAid($IntID));//tag标签
			$nickname=$this->users->getOneByID($artme['adduser']);
			$artme['nickname']=$nickname['nickname'];
			$this->tpl->assign('artme',$artme);
            $this->tpl->assign('MyCat',$MyCat);
			$this->tpl->assign('SeoTitle',$artme['seo_title']=$artme['seo_title']!=""?$artme['seo_title']:$artme['title']);
			$this->tpl->assign('SeoKeywords',$artme['seo_keywords']=$artme['seo_keywords']!=""?$artme['seo_keywords']:$artme['seo_tags']);
            if(empty($artme['seo_description'])){
                $artme['seo_description']=substr(strip_tags(html_entity_decode($Content)),0,250);
            }
			$this->tpl->assign('SeoDescription',$artme['seo_description']);
			if(empty($MyCat['article_skin'])){
				$MyCat['article_skin']='article_view';
			}
			$this->tpl->display($MyCat['article_skin']);
		}else{
			header("location:/404.html");
		}
	}

	private function search(){
		$this->tpl->assign("FormTitle",'文章搜索');
		$this->tpl->assign("FormAction","/search-act.html");
		$this->tpl->display('search_index');
	}

	private function searchkey(){
		$key=urlencode($_POST['searchkeywords']);
		if($key){
			header("refresh:0;url=/search/$key.html");
		}else{
			$message = array(
				'statusCode' =>300,
				"message" => "亲,请不要调戏我",
				"callbackType" =>'forward',
				"forwardUrl" =>"/search.html"
			);
			$this->G->R($message);
		}		
	}

	private function searchlist(){
		if($this->routes->url(2)=="search"){
			$key=$this->routes->get('key');
			$page=$this->routes->get('page');
			$page=$page>0?$page:1;
			$ListAll=$this->search->getAllArtByKey($key,$page);
			$this->tpl->assign("FormTitle",'搜索结果');
			$this->tpl->assign('key',$key);
			$this->tpl->assign('ListAll',$ListAll);
			$this->tpl->assign('SeoTitle','搜索关键词"'.$key.'"的结果 第'.$page.'页');
			$this->tpl->assign('SeoKeywords',$key);
			$this->tpl->display('search_list');
		}
	}

	private function tag(){
		$ListAll=$this->tags->getAll();
		if($ListAll){
			$this->tpl->assign("FormTitle",'tags标签');
			$this->tpl->assign('SeoTitle','tags标签首页');
			$this->tpl->assign("FormAction","tags-act.html");
			$this->tpl->assign('ListAll',$ListAll);
			$this->tpl->display('tags_index');
		}
	}

	//ID形式
	private function tag_id(){
		$tagid=$this->routes->get('tagid');
		$ListOne=$this->tags->getTagByID($tagid);
		$ids=$ListOne['tag_article'];
		$ListAll=$this->article->getAll("FIND_IN_SET(id,'".$ids."')");
		$this->tpl->assign("FormTitle",'tags标签');
		$this->tpl->assign('SeoTitle',$ListOne['tag_label'].'-tags标签页');
		$this->tpl->assign('stag',$ListOne['tag_label']);
		$this->tpl->assign('ListAll',$ListAll);
		$this->tpl->display('tags_list');
	}

	//关键词形式
	//伪静态地址：http://www.chaojicms.com/tags/key=关键词
	private function tag_key(){
		$key=$this->routes->get('key');
		$ListOne=$this->tags->getTagByLabel($key);
		$page = $this->routes->get('page');
		$page = $page>0?$page:1;
		$ids=$ListOne['tag_article'];
		$ListAll=$this->article->getList($page,8,"FIND_IN_SET(id,'".$ids."')");
		$this->tpl->assign("FormTitle",'tags标签为【'.$ListOne['tag_label'].'】的相关文章');
		$this->tpl->assign('SeoTitle',$ListOne['tag_label'].'-tags标签页 第'.$page.'页');
		$this->tpl->assign('stag',$ListOne['tag_label']);
		$this->tpl->assign('ListAll',$ListAll);
		$this->tpl->display('tags_list');
	}
}
?>
